<?php
$id = isset($_GET['id']) ? $_GET['id'] : '';
if (''===$id) die('no id given');
define('_HFMAD_BASE', 'http://hfmad.adsame.com/');
define('_LOGIN_URL', _HFMAD_BASE.'login.php');
define('_PREVIEW_URL', _HFMAD_BASE.'connect.php?id='.$id.'&type=banner&moduleId=banner&action=preview');
define('_USERAGENT', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:9.0.1) Gecko/20100101 Firefox/9.0.1');
define('_HFMAD_USER', 'huiding');
define('_HFMAD_PASS', 'huiding');
define('_COOKIE_FILE', '/tmp/hfm_cookie.txt');

$ch = curl_init(_PREVIEW_URL);
curl_setopt($ch, CURLOPT_COOKIEJAR, _COOKIE_FILE);
curl_setopt($ch, CURLOPT_COOKIEFILE, _COOKIE_FILE);
curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$rtn = curl_exec($ch);//try to get preview with old session id
if ($rtn!='{nosession:true}') {
    $rtn = str_replace(array("<div class='formatdiv'><img src='resources/images/adsame/adsame-s.jpg'/></div>",'<div style="width:900px;height:40px;margin-left:auto;margin-right:auto;margin-top:20px;"></div><div id="ad_TOP" style="width:900px;height:40px;margin-left:auto;margin-right:auto;"></div><div style=\'left:0;top:0;position:absolute;width:100%;height:100%;z-index:-1;opacity:0.1;filter:alpha(opacity=10);\'></div>',"<br />"), "", $rtn);
    die($rtn.'<!--sid cached-->');//got it
}
curl_setopt($ch, CURLOPT_URL, _LOGIN_URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'module=login&user='._HFMAD_USER.'&pass='._HFMAD_PASS.'&lang=cn&sn=');
$rtn = curl_exec($ch);//login
if (strpos($rtn,'{success:true')!==0) die('login failed. msg:'.$rtn);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_URL, _PREVIEW_URL);
$rtn = curl_exec($ch);//get preview result
$rtn = str_replace(array("<div class='formatdiv'><img src='resources/images/adsame/adsame-s.jpg'/></div>",'<div style="width:900px;height:40px;margin-left:auto;margin-right:auto;margin-top:20px;"></div><div id="ad_TOP" style="width:900px;height:40px;margin-left:auto;margin-right:auto;"></div><div style=\'left:0;top:0;position:absolute;width:100%;height:100%;z-index:-1;opacity:0.1;filter:alpha(opacity=10);\'></div>',"<br />"), "", $rtn);
die($rtn);
